package love.contact.engine;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import wwk.common.constant.Commonstant;
import wwk.common.event.MessageManager;
import wwk.common.phone.SuspensionWindow;
import love.contact.R;
import love.contact.data.Contact;
import love.contact.data.Group;
import love.contact.data.PhoneBook;

/**
 * @author wangweike
 * @create_date 2011-12-22 10:35:57
 * @update_date 2011-12-22 10:35:57
 */

public class Engine
{
	private Context context;
	private PhoneBook phoneBook;
	public Group currentGroup;
	private SuspensionWindow suspensionWindow; 
	//private boolean contactChanged;
	//private ContactContentObserver contactContentObserver;
	public String pendingFilePathName;
	
	private Handler	handler=new Handler() 
	{
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			
			//Context context=null;//HandlerCenter.instance().getContext();
			
			int messageType=msg.what;
			
			switch (messageType)
			{
				case Commonstant.Message_Phone_Outcall:
					{
						phoneBook.openOrCreateDatabase(context);
						
						String number=(String)msg.obj;
						String text=context.getString(R.string.ungroup);
						Object[] objs=phoneBook.getContactInfo(context, number);
						Contact contact=(Contact)objs[0];
						if(contact!=null)
						{
							String groupName=(String)objs[1];
							if(groupName!=null)
							{
								text=context.getString(R.string.group)+":"+groupName;
							}
						}
						
						if(suspensionWindow==null)
						{
							suspensionWindow = new SuspensionWindow();
						}
						
						suspensionWindow.showText(context, text, 
								context.getResources().getDrawable(R.drawable.round)); 
						//Toast.makeText(context, "Message_Phone_Outcall", Toast.LENGTH_SHORT).show();
					}
					break;
				case Commonstant.Message_Phone_Incoming:
					{
						phoneBook.openOrCreateDatabase(context);
						
						String number=(String)msg.obj;
						Object[] objs=phoneBook.getContactInfo(context, number);
						Contact contact=(Contact)objs[0];
						String text=context.getString(R.string.ungroup);
						if(contact!=null)
						{
							String groupName=(String)objs[1];
							if(groupName!=null)
							{
								text=context.getString(R.string.group)+":"+groupName;
							}
						}
	
						if(suspensionWindow==null)
						{
							suspensionWindow = new SuspensionWindow();
						}
						
						suspensionWindow.showText(context, text, 
								context.getResources().getDrawable(R.drawable.round)); 
						//Toast.makeText(context, "Message_Phone_Incoming", Toast.LENGTH_SHORT).show();
					}
					break;
				case Commonstant.Message_Phone_Idle:
					{
						if(suspensionWindow!=null)
						{
							suspensionWindow.hide();
							//Toast.makeText(context, "Message_Phone_Idle", Toast.LENGTH_SHORT).show();
						}
						else
						{
							//Toast.makeText(context, "Message_Phone_Idle suspensionWindow NULL", Toast.LENGTH_SHORT).show();
						}
					}
					break;
//				case Message_Contact_Changed:
//					{
//						contactChanged=true;
//					}
//					break;
			}
		}
	};

	public Engine()
	{
		phoneBook=new PhoneBook();
		resetCurrentGroup();
		
		MessageManager.instance().addHandler(handler, Commonstant.Message_Phone_Idle);
		MessageManager.instance().addHandler(handler, Commonstant.Message_Phone_Incoming);
		MessageManager.instance().addHandler(handler, Commonstant.Message_Phone_Outcall);
		MessageManager.instance().addHandler(handler, Commonstant.Message_Contact_Changed);
	}
	
	public void setContext(Context context)
	{
		this.context=context;
	}
	
	public void resetCurrentGroup()
	{
		if(currentGroup!=null)
		{
			currentGroup.closeCusror();
		}
		currentGroup=new Group(null,null,phoneBook.getSqliteUtil());//����
	}

	public PhoneBook getPhoneBook() {
		return phoneBook;
	}
	
	/*
	public void unregisterContentObserver(Context context)
	{
		onStopCount--;
		
		if(contactContentObserver!=null)
		{
			if(onStopCount<=0)
			{
				context.getContentResolver().unregisterContentObserver(contactContentObserver);
			}
		}
		
		if(contactChanged)
		{
			//Toast.makeText(context, "Contacts data modified", Toast.LENGTH_LONG);
			CustomDialog.showAlertDialogWithBlock(context,"Contacts data modified,reload");
			//boolean ret=CustomDialog.showSelectDialog(context, "Contacts data modified,reload");
			//if(ret)
			{
				Intent intent=new Intent(context,ILoveContactActivity.class);
				context.startActivity(intent);
				//context.finish();
				
				//forbidden by system
				//ActivityManager am = (ActivityManager)rootActivity.getSystemService(Context.ACTIVITY_SERVICE);    
	            //am.restartPackage(rootActivity.getPackageName());
			}
			
			contactChanged=false;
		}
	}
	
	public void registerContentObserver(Context context)
	{
		onStopCount++;
		
		if(contactContentObserver==null)
		{
			contactContentObserver = new ContactContentObserver(handler);
		}
		if(onStopCount>0)
		{
			context.getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactContentObserver); 
		}
	}
  */
	
}

/*
class ContactContentObserver extends ContentObserver
{
	Handler handler;
    public ContactContentObserver(Handler handler) 
    {
   	 	super(null);
   	 	
   	 	this.handler=handler;
    }
    
    @Override
    public void onChange(boolean selfChange) 
    {
        super.onChange(selfChange);
        
        Log.d("ContentObserver","onChange "+selfChange);
        
        Message msg = new Message();
		msg.what = MessageType.Message_Contact_Changed.ordinal();
		HandlerCenter.instance().sendMessage(msg);
    }
}
*/
